# Maintainer: Tobias Powalowski <tpowa@archlinux.org>
# Maintainer: Thomas Bächler <thomas@archlinux.org>
# Maintainer: Anatol Pomozov <anatol.pomozov@gmail.com>
# Contributor: Keshav Amburay <(the ddoott ridikulus ddoott rat) (aatt) (gemmaeiil) (ddoott) (ccoomm)>

pkgname=syslinux
pkgver=6.04.pre2.r11.gbf6db5b4
#_tag=syslinux-$pkgver
_commit=bf6db5b4
pkgrel=3
pkgdesc='Collection of boot loaders that boot from FAT, ext2/3/4 and btrfs filesystems, from CDs and via PXE'
url='https://www.syslinux.org/'
arch=(x86_64)
backup=(boot/syslinux/syslinux.cfg)
install=syslinux.install
license=(GPL2)
# syslinux build system is a mess of submakes that does not work with -jN
# efi32/com32 do not like Arch cflags/ldflags, though it would be nice to have the flags for userspace tools
options=(!makeflags !buildflags)
makedepends=(git python nasm upx asciidoc)
makedepends_x86_64=(lib32-glibc) # efi32 needs it
optdepends=('perl-crypt-passwdmd5: For md5pass'
            'perl-digest-sha1:     For sha1pass'
            'mtools:               For mkdiskimage and syslinux support'
            'gptfdisk:             For GPT support'
            'util-linux:           For isohybrid'
            'efibootmgr:           For EFI support'
            'dosfstools:           For EFI support')

# The syslinux-install_update script is maintained at https://gist.github.com/pyther/772138
# Script not yet updated for syslinux-efi
source=(git+https://repo.or.cz/syslinux.git#commit=$_commit
        syslinux.cfg
        syslinux-install_update
        0002-gfxboot-menu-label.patch
        0005-gnu-efi-version-compatibility.patch
        0017-single-load-segment.patch
        0004-gnu-efi-from-arch.patch
        0016-strip-gnu-property.patch
        0018-prevent-pow-optimization.patch
        0025-reproducible-build.patch
)
sha1sums=('SKIP'
          '1145f454bd297d373ad123425f93620c3e92f585'
          'df5160a138ca8c6502d67fe1a64fec78b50e82c2'
          '69239fda4a3d5a247f0e48c5c683e0124c7d9201'
          '1e0cd52252622f206d22e84f2a4d358508c902f4'
          '81d05572b15dc5c55b63489fecdc79a6a332959c'
          '4c3706e7a5620e450a6abb9238c08d7b39bb6945'
          'f4a6768c7e5b8d02428ae550e37a7ca445ad1bb8'
          'c94622eaa14ad9512e4f43b13302ba35d1787cbc'
          '5b3878a1fff74bbb09ddbc66cc8afa38f34ed05c')

_targets='bios efi64 efi32'

pkgver() {
  cd syslinux
  git describe --long | sed 's/^syslinux-//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd syslinux

  patch -p1 < ../0002-gfxboot-menu-label.patch
  patch -p1 < ../0017-single-load-segment.patch
  patch -p1 < ../0016-strip-gnu-property.patch
  patch -p1 < ../0018-prevent-pow-optimization.patch

  # TODO: use 'gnu-efi-libs' package instead of embedded sources
  # patch -p1 < ../0004-gnu-efi-from-arch.patch
  # patch -p1 < ../0005-gnu-efi-version-compatibility.patch

  patch -p1 < ../0025-reproducible-build.patch

  # do not swallow efi compilation output to make debugging easier
  sed 's|> /dev/null 2>&1||' -i efi/check-gnu-efi.sh

  # disable debug and development flags to reduce bootloader size
  truncate --size 0 mk/devel.mk
}

build() {
  cd syslinux
  export LDFLAGS+=--no-dynamic-linker  # workaround for binutils 2.28 http://www.syslinux.org/wiki/index.php?title=Building
  export EXTRA_CFLAGS=-fno-PIE   # to fix gpxe build
  make PYTHON=python $_targets
}

package() {
  cd syslinux
  make $_targets install INSTALLROOT="$pkgdir" SBINDIR=/usr/bin MANDIR=/usr/share/man AUXDIR=/usr/lib/syslinux

  rm -r "$pkgdir"/usr/lib/syslinux/{com32,dosutil,syslinux.com}
  install -D -m644 COPYING "$pkgdir"/usr/share/licenses/syslinux/COPYING
  install -d "$pkgdir"/usr/share/doc
  cp -ar doc "$pkgdir"/usr/share/doc/syslinux

  install -d "$pkgdir"/usr/lib/syslinux/bios
  mv "$pkgdir"/usr/lib/syslinux/{*.bin,*.c32,*.0,memdisk} "$pkgdir"/usr/lib/syslinux/bios 

  install -D -m0644 ../syslinux.cfg "$pkgdir"/boot/syslinux/syslinux.cfg
  install -D -m0755 ../syslinux-install_update "$pkgdir"/usr/bin/syslinux-install_update
}
